package com.qingcheng.controller;

import com.qingcheng.entity.Result;
import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.LoginLog;
import com.qingcheng.service.LoginLogService;
import com.qingcheng.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private LoginLogService loginLogService;

    @GetMapping("/login")
    public Result queryLogin(String loginName, String password){
        Admin admin = this.loginService.queryLogin(loginName);
        if (admin==null){
            return new Result(401,"没有该账户,请进行注册",null);
        }
        if (!BCrypt.checkpw(password,admin.getPassword())){
            return new Result(400,"密码错误",null);
         }
        LoginLog loginLog = new LoginLog();
        loginLog.setLoginName(loginName);
        loginLogService.add(loginLog);
        return new Result(200,"登陆成功",admin);
    }
}
